package com.example.mvp.delegate

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.baselibrary.R

/**
 * 上传图片适配器视图托管
 * @author  chaohaipeng  on 2018/8/9.
 */
class UPImageDelegate: BaseAdapterDelegate(){
    /**
     * 图片加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()
    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.adapter_up_image
    }
    /**
     * 初始化数据View
     */
    fun initDataView(context: Context){
        val iv_afbdi_image = getView<ImageView>(R.id.iv_up_images)
        val iv_up_delete_btn = getView<ImageView>(R.id.iv_up_delete_btn)
        Glide.with(context).clear(iv_afbdi_image)
        val layoutParams: RelativeLayout.LayoutParams = iv_afbdi_image.layoutParams as RelativeLayout.LayoutParams
        layoutParams.width=LKScreenUtil.dp2px(100f)
        layoutParams.height=LKScreenUtil.dp2px(100f)
        iv_afbdi_image.layoutParams=layoutParams
        iv_afbdi_image.setImageResource(R.mipmap.icon_up_image)
        iv_up_delete_btn.visibility= View.GONE
    }
    /**
     * 初始化数据
     */
    fun initData(context: Context, imagePath:String){
        val iv_afbdi_image = getView<ImageView>(R.id.iv_up_images)
        val iv_up_delete_btn = getView<ImageView>(R.id.iv_up_delete_btn)
        val layoutParams: RelativeLayout.LayoutParams = iv_afbdi_image.layoutParams as RelativeLayout.LayoutParams
        layoutParams.width=LKScreenUtil.dp2px(100f)
        layoutParams.height=LKScreenUtil.dp2px(100f)
        iv_afbdi_image.layoutParams=layoutParams
        setImage(context,imagePath ,iv_afbdi_image)
        iv_up_delete_btn.visibility= View.VISIBLE
    }


    private fun setImage(context: Context, imagePath:String, imageView: ImageView){
        val mRequestManager = Glide.with(context)
        mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        mRequestOptions.skipMemoryCache(true)
        mRequestManager?.load(imagePath)
                ?.thumbnail(0.2f)
                ?.apply(mRequestOptions)
                ?.into(imageView)
        imageView.setTag(R.id.tag_image,imagePath)


    }

}